import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/model/app_info.dart';
import 'package:flutter_eve_plugin/model/health/user_info.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/login/login.dart';
import 'package:flutter_eve_plugin/pages/main/actions.dart';
import 'package:flutter_eve_plugin/pages/ye_xiu/login/actions.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/route/main.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/store_vm/ye_xiu/login/main.dart';
import 'package:flutter_eve_plugin/store_vm/ye_xiu/main.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

class LoginBtnWidget extends StatelessWidget {
  const LoginBtnWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(
        converter: (store) => AppStateViewModel(
            state: store.state,
            yeXiuStateViewModel: YeXiuStateViewModel(
                loginStateViewModel: LoginStateViewModel(onLogin: (req) {
              PluginEve.sendMessage(PluginConstant.httpYXRegMobile,
                      param: req, httpType: HttpType.POST, context: context)
                  .then((data) {
                AccountInfo info = data;
                loginDispatch(store, info);
                switch (info?.userStage) {
                  case 2:
                    Navigator.pushNamed(
                        context, RouteConstant.LoginBirthdayPage);
                    break;
                  case 3:
                    {
                      if (info.deviceId != 0) {
                        if (info.idol == 'DAYEXIU') {
                          ///大叶修
                          ///调转主页
                        } else if (info.idol == 'C2') {
                          ///C2
                          ///调转主页
                          goC2HomeNative();
                        } else if (info.idol == 'YEXIU') {
                          ///叶修
                          ///调转主页
                        } else {
                          Navigator.pushNamed(
                              context, RouteConstant.LoginSelectMachinePage);
                        }
                      } else {
                        Navigator.pushNamed(
                            context, RouteConstant.LoginSelectMachinePage);
                      }
                    }
                    break;
                }
              });
            }))),
        builder: (context, vm) {
          bool isConfirm = false;
          String phone = vm.state.yeXiuState.loginState.phone ?? '';
          String code = vm.state.yeXiuState.loginState.code ?? '';
          String areaCode = vm.state.yeXiuState.loginState.areaCode ?? '86';
          if (phone.length > 0 && code.length > 0 && areaCode.length > 0) {
            isConfirm = true;
          }
          return GestureDetector(
            onTap: isConfirm
                ? () {
                    PluginEve.sendMessage(PluginConstant.httpYXAppInfo, httpType: HttpType.POST)
                        .then((data) {
                      AppInfo info = data;
                      var req = LoginReq(
                          vm.state.yeXiuState.loginState.phone,
                          vm.state.yeXiuState.loginState.code,
                          Platform.isIOS ? 1 : 2,
                          info.versionCode,
                          vm.state.yeXiuState.loginState.areaCode);
                      vm.yeXiuStateViewModel.loginStateViewModel.onLogin(req);
                    });
                  }
                : null,
            child: Container(
              margin: EdgeInsets.only(left: setRatio(30), right: setRatio(30)),
              height: setRatio(46),
              decoration: isConfirm
                  ? BoxDecoration(
                      borderRadius:
                          BorderRadius.all(Radius.circular(setRatio(5))),
                      gradient: LinearGradient(
                          colors: [Color(0xFF0386FF), Color(0xFF03AAFF)],
                          begin: Alignment(0.5, 0),
                          end: Alignment(0.5, 1)))
                  : BoxDecoration(
                      color: Color(0xFFD8D8D8),
                      borderRadius:
                          BorderRadius.all(Radius.circular(setRatio(5))),
                    ),
              child: Center(
                child: Text(
                  '登录',
                  style: TextStyle(
                      fontSize: setTextSp(17), color: Color(0xFFFFFFFF)),
                ),
              ),
            ),
          );
        });
  }
}
